﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using Microsoft.AspNetCore.Mvc;
using Iservices;
using Model;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;

namespace Hr.Controllers
{
    public class client1Controller : Controller
    {
        private readonly ILogger<client1Controller> _logger;
        private readonly Iconfig_public_charServices iengage_Major;
        private readonly Iconfig_major_kindServices iconfig_Major;
        private readonly Iconfig_majorServices iconfig;
        private readonly MyDbContext myDbContext;

        public client1Controller(MyDbContext myDbContext, ILogger<client1Controller> logger, Iconfig_public_charServices iengage_Major, Iconfig_major_kindServices iconfig_Major, Iconfig_majorServices iconfig)
        {
            this.myDbContext = myDbContext;
            _logger = logger;
            this.iengage_Major = iengage_Major;
            this.iconfig_Major = iconfig_Major;
            this.iconfig = iconfig;
        }
        //查看职位设置
        [HttpPost]
        public IActionResult profession_designs()
        {
          // List<config_public_char> list = await iengage_Major.config_public_charSelect();
            var list = myDbContext.config_public_char.
                 Where(e => e.attribute_kind.Contains("职称"))
                 .AsNoTracking()//查询的结果集是单个实体
                 .Select(e => e)
                 .ToList();
             return Ok(list);
        }
        [HttpGet]
        public IActionResult profession_design()
        {
            return View();
        }

        //删除职位设置
        [HttpDelete]
        public async Task<int> profession_designDelete([FromForm] int id)
        {
            int result = 0;
            try
            {
                Econfig_public_char test = await myDbContext.config_public_char.FindAsync(id);
                myDbContext.config_public_char.Remove(test);
                result = await myDbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex.Message);
            }
            return result;

        }

        //查看职位分类设置
        [HttpPost]
        public async Task<IActionResult> major_kinds()
        {
            List<config_major_kind> list = await iconfig_Major.config_major_kindSelect();
            return Ok(list);
        }
        [HttpGet]
        public IActionResult major_kind()
        {
            return View();
        }
        //增加职位分类设置
        [HttpPost]
        public async Task<ActionResult> major_kind_add([FromForm] Econfig_major_kind test)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    myDbContext.config_major_kind.Add(test);
                    result = await myDbContext.SaveChangesAsync();
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            return Content(result.ToString());
        }
        [HttpGet]
        public ActionResult major_kind_add()
        {
            return View();
        }
        //删除职位分类设置
        [HttpDelete]
        public async Task<int> major_kindsDelete([FromForm] int id)
        {
            int result = 0;
            try
            {
                Econfig_major_kind test = await myDbContext.config_major_kind.FindAsync(id);
                myDbContext.config_major_kind.Remove(test);
                result = await myDbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex.Message);
            }
            return result;

        }

        //查看职位设置
        [HttpPost]
        public async Task<IActionResult> majors()
        {
            List<config_major> list = await iconfig.config_majorSelect();
            return Ok(list);
        }
        [HttpGet]
        public IActionResult major()
        {
            return View();
        }
        //增加职位设置
        [HttpPost]
        public async Task<ActionResult> major_add([FromForm] Econfig_major test)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    myDbContext.config_major.Add(test);
                    result = await myDbContext.SaveChangesAsync();
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            return Content(result.ToString());
        }
        [HttpGet]
        public ActionResult major_add()
        {
            return View();
        }
        //删除职位设置
        [HttpDelete]
        public async Task<int> major_Delete([FromForm] int id)
        {
            int result = 0;
            try
            {
                Econfig_major test = await myDbContext.config_major.FindAsync(id);
                myDbContext.config_major.Remove(test);
                result = await myDbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex.Message);
            }
            return result;

        }
    }
}

